package nz.ac.massey.jbuif.events;

import java.util.Collection;
import java.util.Collections;
import java.util.Vector;

/**
 * Class BeanActionSupport
 * @author AMIR H KIOUMARS
 *
 */
public class BeanActionSupport {
	private Collection<BeanActionListener> listeners;
	
	
	/**
	 * BeanActionSupport
	 */
	public BeanActionSupport() {
		listeners = new Vector<BeanActionListener>();
	}
	
	
	/**
	 * addBeanActionListener add the bean action listener
	 * @param listener BeanActionListener instance
	 */
	public void addBeanActionListener(BeanActionListener listener) {
		listeners.add(listener);
	}
	
	
	/**
	 * removeBeanActionListener remove the bean action listener
	 * @param listener BeanActionListener instance
	 */
	public void removeBeanActionListener(BeanActionListener listener) {
		listeners.remove(listener);
	}
	
	
	/**
	 * beanActionEvent controls the bean action event
	 * @param e BeanActionEvent instance
	 */
	private void beanActionEvent(BeanActionEvent e) {
		// create a copy of the listeners
		Collection<BeanActionListener> lstnrs = Collections.unmodifiableCollection(listeners);
		
		for(BeanActionListener bal : lstnrs)
			bal.onBeanAction(e);
	}

	
	/**
	 * fireBeanActionEvent call the fireBeanActionEvent override method
	 * @param e BeanActionEvent instance
	 */
	public void fireBeanActionEvent(BeanActionEvent e) {
		beanActionEvent(e);
	}
	
	
	/**
	 * fireBeanActionEvent controls the fired bean action event
	 * @param source object source
	 * @param bean object bean
	 * @param beanAction BeanActions instance
	 */
	public void fireBeanActionEvent(Object source, Object bean, BeanActions beanAction) {
		BeanActionEvent bae = new BeanActionEvent(source, bean, beanAction);
		beanActionEvent(bae);
	}
}
